package com.fivefaces.ide.plugins.impl;

import com.fivefaces.ide.plugins.ConsoleExecutablePathService;
import com.fivefaces.ide.plugins.Constants;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * User: abdullahkhan
 * Date: 13/09/12
 * Time: 10:22 PM
 */
public abstract class AbstractConsoleExecutablePathService implements ConsoleExecutablePathService {
    protected final Properties properties;
    private final String configRootPath = System.getProperty("user.home") + File.separator + ".console-plugin";
    private final String configLocation = configRootPath + File.separator + "settings.properties";

    protected AbstractConsoleExecutablePathService() throws IOException {
        properties = new Properties();
        if (!new File(configRootPath).exists()) {
            new File(configRootPath).mkdir();
        }
        if (!new File(configLocation).exists()) {
            new File(configLocation).createNewFile();
        }

        properties.load(new FileInputStream(configLocation));
    }

    @Override
    public boolean save(String path) throws IllegalAccessException, IOException {
        boolean result = true;
        properties.put(Constants.EXECUTABLE_PATH_KEY, path);
        properties.store(new FileOutputStream(configLocation), "");
        return result;
    }
}
